package com.zzyl.serve.service.impl;

import java.util.List;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zzyl.common.constant.CacheConstants;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.common.utils.StringUtils;
import com.zzyl.serve.dto.PageQueryDTO;
import com.zzyl.serve.vo.NursingProjectVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.zzyl.serve.mapper.NursingProjectMapper;
import com.zzyl.serve.domain.NursingProject;
import com.zzyl.serve.service.INursingProjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.Arrays;

import static com.zzyl.common.constant.CacheConstants.NURSING_PROJECT_ALL_KEY;

/**
 * 护理项目Service业务层处理
 *
 * @author ruoyi
 * @date 2024-09-30
 */
@Service
public class NursingProjectServiceImpl extends ServiceImpl<NursingProjectMapper, NursingProject> implements INursingProjectService {
    @Autowired
    private NursingProjectMapper nursingProjectMapper;

    @Autowired
    private RedisTemplate<Object, Object> redisTemplate;

    /**
     * 查询护理项目
     *
     * @param id 护理项目主键
     * @return 护理项目
     */
    @Override
    public NursingProject selectNursingProjectById(Long id) {
        return getById(id);
    }

    /**
     * 查询护理项目列表
     *
     * @param nursingProject 护理项目
     * @return 护理项目
     */
    @Override
    public List<NursingProject> selectNursingProjectList(NursingProject nursingProject) {
        return nursingProjectMapper.selectNursingProjectList(nursingProject);
    }

    /**
     * 新增护理项目
     *
     * @param nursingProject 护理项目
     * @return 结果
     */
    @Override
    public int insertNursingProject(NursingProject nursingProject) {
        redisTemplate.delete(NURSING_PROJECT_ALL_KEY);
        return save(nursingProject) ? 1 : 0;
    }

    /**
     * 修改护理项目
     *
     * @param nursingProject 护理项目
     * @return 结果
     */
    @Override
    public int updateNursingProject(NursingProject nursingProject) {
        redisTemplate.delete(NURSING_PROJECT_ALL_KEY);
        return updateById(nursingProject) ? 1 : 0;
    }

    /**
     * 批量删除护理项目
     *
     * @param ids 需要删除的护理项目主键
     * @return 结果
     */
    @Override
    public int deleteNursingProjectByIds(Long[] ids) {
        redisTemplate.delete(NURSING_PROJECT_ALL_KEY);
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除护理项目信息
     *
     * @param id 护理项目主键
     * @return 结果
     */
    @Override
    public int deleteNursingProjectById(Long id) {
        redisTemplate.delete(NURSING_PROJECT_ALL_KEY);
        return removeById(id) ? 1 : 0;
    }

    /**
     * 查询所有项目
     *
     * @return
     */
    @Override
    public List<NursingProjectVO> selectAll() {
        //查询缓存中是否含有数据
        List<NursingProjectVO> nursingProjectVOS = (List<NursingProjectVO>) redisTemplate.opsForValue().get(NURSING_PROJECT_ALL_KEY);
        //判断是否为空
        if (ObjectUtil.isNotNull(nursingProjectVOS)) {
            //不为空则直接返回
            return nursingProjectVOS;
        }
        //为空，则从数据库中查询数据并存入在缓存中
        List<NursingProjectVO> list = nursingProjectMapper.selectAll();
        redisTemplate.opsForValue().set(NURSING_PROJECT_ALL_KEY, list);
        return list;
    }

    /**
     * 小程序分页查询
     *
     * @param pageQueryDTO
     */
    @Override
    public Page<NursingProject> pageQuery(PageQueryDTO pageQueryDTO) {
        //构建Ipage对象
        Page<NursingProject> page = new Page<>(pageQueryDTO.getPageNum(), pageQueryDTO.getPageSize());
        //条件查询
        Page<NursingProject> projectPage = this.lambdaQuery()
                .like(StringUtils.isNotEmpty(pageQueryDTO.getName()), NursingProject::getName, pageQueryDTO.getName())
                .eq(ObjectUtil.isNotNull(pageQueryDTO.getStatus()), NursingProject::getStatus, pageQueryDTO.getStatus())
                .page(page);
        return projectPage;
    }
}
